#pragma once  

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
   
#include <map>
#include <vector>
 
#include "fore_rtdb.h"
#include "./base.h"
 
#include "./rtdbFun.h" 

 


union SUBSCRIBE_KEY
{
	int  key;
	struct key_item
	{
		unsigned char  res1;
		unsigned char  res2;
		unsigned char  res3;  
		unsigned char  res4; 
	} id;
 
	SUBSCRIBE_KEY(unsigned char ID1, unsigned char ID2, unsigned char ID3, unsigned char ID4  )
	{
		id.res1 = ID1; id.res2 = ID2; id.res3 = ID3;  id.res4 = ID4; 
	}

	SUBSCRIBE_KEY( int keyId)
	{
		key = keyId;
	}
};

   
#pragma pack(1)


struct RT_DATA
{  
	char  dev_type[32];
	char  dev_code[64];
	char  field_name[32]; 
	char  serverId[32]; 
	short data_type;
	float fval;
	int   update_time;
};
typedef std::vector<RT_DATA> ARYRTDATA;
 

struct APP_PARAM_CFG
{  
	char  param_key[32];
	char  param_val[32];
};
typedef std::vector<APP_PARAM_CFG>   ARY_PARAM_CFG;

struct APP_PARAM_QUERY
{
	char  query_param_reply[32];
	ARY_PARAM_CFG    aryParam;
};

struct REG_ADDR
{  
	REG_ADDR()
	{
		memset(this, 0, sizeof(REG_ADDR));
	}
	int   addr ;
	char  pmsId[48]; 
};


#pragma pack()



namespace TDbFun
{
	 
	// 
	void load_rtdata( char * nodeID, ARYMEATEMPLATE & ary, std::vector<RT_DATA> & rtData);
	//
	void load_rtdata_bychanged( std::vector<RT_DATA> & rtDataOld, std::vector<RT_DATA> & rtDataChanged, int readDataType = 0);  //0:all, 1:yc, 2:yx
	  
	bool put_yx(char * appName, time_t updateTime, char* stringkey, unsigned char val);     
	bool put_yc(char * appName, time_t updateTime, char* stringkey, float val);               
 
	bool GetDevs_ByDevType(char *chrDevType, std::vector<FORE_DEV_DICT> & aryDev);   

	void GetDevs(std::vector<FORE_DEV_DICT> & aryDev);

	bool get_paramval_by_key( char * chrParamKey, char * chrVal );

	///////////////////////////
	void query_meaTempl_by_devType(char* dev_type, std::vector<FORE_MEA_TEMPLATE> & aryMeaTemplInfo);
	bool get_mea_template_by_devTypeAndfieldName( char * dev_type, char* field_name, FORE_MEA_TEMPLATE & mea); 

	void query_meaTempl_by_devType(char* dev_type, std::map<string, ARYMEATEMPLATE> & mapServiceID2Meatempl);

	void remove_mev_by_dev_type(char* dev_type);
	
	void GetDevType(std::vector<FORE_DEV_TYPE> & aryDevType);
	
	bool set_rtdb_version( unsigned int ver );
	int  get_rtdb_Version();
};
